﻿using UnityEngine;
using System.Collections;

[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
public class PointRender : MonoBehaviour {
    private Mesh mesh;

    private LoadModel.VetexAttrib[] points;
    public float pointSize = 1;
    public LoadModel loadModel;
	// Use this for initialization
	void Start () {
        mesh = new Mesh();
        GetComponent<MeshFilter>().mesh = mesh;
        CreateMesh();
	}


    [ButtonCallFunc()]
    public bool RenderAni;
    /// <summary>
    /// LoadModel
    /// 加载模型
    /// 加载骨骼
    /// 加载动画
    /// 渲染动画
    /// 
    /// 接着执行billboard的RenderAniMethod
    /// </summary>
    public void RenderAniMethod()
    {
        points = loadModel.verAttribs;
        var aniPointPos = loadModel.aniVertexPos;

        var triangles = new Vector3[points.Length * 4];
        var indecies = new int[points.Length * 6];
        var colors = new Color[points.Length * 4];
        var normals = new Vector3[points.Length * 4];

        for (var i = 0; i < points.Length; i++)
        {
            var point = points[i];
            var p = aniPointPos[i];
            var lb = p + new Vector3(-pointSize, -pointSize, 0);
            var lt = p + new Vector3(-pointSize, pointSize, 0);
            var rb = p + new Vector3(pointSize, -pointSize, 0);
            var rt = p + new Vector3(pointSize, pointSize, 0);
            triangles[4 * i + 0] = lb;
            triangles[4 * i + 1] = rb;
            triangles[4 * i + 2] = rt;
            triangles[4 * i + 3] = lt;

            colors[4 * i + 0] = point.color;
            colors[4 * i + 1] = point.color;
            colors[4 * i + 2] = point.color;
            colors[4 * i + 3] = point.color;

            normals[4 * i + 0] = p;
            normals[4 * i + 1] = p;
            normals[4 * i + 2] = p;
            normals[4 * i + 3] = p;

            indecies[6 * i + 0] = 4 * i + 0;
            indecies[6 * i + 1] = 4 * i + 2;
            indecies[6 * i + 2] = 4 * i + 1;

            indecies[6 * i + 3] = 4 * i + 0;
            indecies[6 * i + 4] = 4 * i + 3;
            indecies[6 * i + 5] = 4 * i + 2;
        }
        mesh.vertices = triangles;
        mesh.colors = colors;
        mesh.normals = normals;
        mesh.SetIndices(indecies, MeshTopology.Triangles, 0);
    }


    void CreateMesh()
    {
        if(loadModel != null)
        {
            points = loadModel.verAttribs;
        }else
        {
            return;
        }
        /*
        points = new Vector3[2];
        points[0] = Vector3.zero;
        points[1] = new Vector3(2, 0, 1);
        */

        var triangles = new Vector3[points.Length*4];
        var indecies = new int[points.Length*6];
        var colors = new Color[points.Length*4];
        var normals = new Vector3[points.Length * 4];

        for(var i = 0; i < points.Length; i++)
        {
            var point = points[i];
            var p = point.position;
            var lb = p + new Vector3(-pointSize, -pointSize, 0);
            var lt = p + new Vector3(-pointSize, pointSize, 0);
            var rb = p + new Vector3(pointSize, -pointSize, 0);
            var rt = p + new Vector3(pointSize, pointSize, 0);
            triangles[4 * i + 0] = lb;
            triangles[4 * i + 1] = rb;
            triangles[4 * i + 2] = rt;
            triangles[4 * i + 3] = lt;

            colors[4 * i + 0] = point.color;
            colors[4 * i + 1] = point.color;
            colors[4 * i + 2] = point.color;
            colors[4 * i + 3] = point.color;

            normals[4 * i + 0] = p;
            normals[4 * i + 1] = p;
            normals[4 * i + 2] = p;
            normals[4 * i + 3] = p;

            indecies[6*i+0] = 4*i+0;
            indecies[6*i+1] = 4*i+2;
            indecies[6*i+2] = 4*i+1;

            indecies[6*i+3] = 4*i+0;
            indecies[6*i+4] = 4*i+3;
            indecies[6*i+5] = 4*i+2;
        }
        mesh.vertices = triangles;
        mesh.colors = colors;
        mesh.normals = normals;
        mesh.SetIndices(indecies, MeshTopology.Triangles, 0);
    }
}
